//
//  ViewController.m
//  MVMMLoginDemo
//
//  Created by jianzhao4 on 2017/9/18.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import <Masonry/Masonry.h>
#import "LoginViewModel.h"
#import <ReactiveObjC/ReactiveObjC.h>

@interface ViewController ()

@property (nonatomic, weak) UITextField *usernameTextField;
@property (nonatomic, weak) UITextField *passwordTextField;
@property (nonatomic, weak) UIButton *loginButton;

@property (nonatomic, strong) LoginViewModel *loginViewModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
    [self bindModel];
}

- (void)setupUI {
    UITextField *usernameTextField = [UITextField new];
    usernameTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:usernameTextField];
    [usernameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).mas_offset(8);
        make.right.equalTo(self.view.mas_right).mas_offset(-8);
        make.top.equalTo(self.mas_topLayoutGuideBottom).mas_offset(8);
    }];
    self.usernameTextField = usernameTextField;
    
    UITextField *passwordTextField = [UITextField new];
    passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:passwordTextField];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(usernameTextField.mas_left);
        make.right.equalTo(usernameTextField.mas_right);
        make.top.equalTo(usernameTextField.mas_bottom).mas_offset(8);
    }];
    self.passwordTextField = passwordTextField;
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setTitle:@"Login" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordTextField.mas_left);
        make.right.equalTo(passwordTextField.mas_right);
        make.top.equalTo(passwordTextField.mas_bottom).mas_offset(8);
    }];
    self.loginButton = loginButton;
}

- (void)bindModel {
    // 模型绑定至视图
    RAC(self.loginViewModel.account, username) = self.usernameTextField.rac_textSignal;
    // 等效于
    // [self.usernameTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
    //     self.loginViewModel.account.username = x;
    // }];
    RAC(self.loginViewModel.account, password) = self.passwordTextField.rac_textSignal;
    RAC(self.loginButton, enabled) = self.loginViewModel.enableLoginSignal;
    
    // 监听登录按钮点击
    [[_loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton * _Nullable x) {
        // 执行登录命令
        [self.loginViewModel.loginCommand execute:nil];
    }];
}

#pragma mark - Lazy Loading

- (LoginViewModel *)loginViewModel {
    if (!_loginViewModel) {
        LoginViewModel *model = [[LoginViewModel alloc] init];
        _loginViewModel = model;
    }
    return _loginViewModel;
}

@end
